﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using Bazookabird.Common;
using Bazookabird.Common.Plugin;
using Bazookabird.Wpf.Model;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace Bazookabird.Wpf.Queries.QueryHandlers.SimpleCmmiProcessTaskViewer
{
    [Export(typeof(IQueryHandler))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class SimpleCmmiProcessTaskViewerViewModel : PanelViewModelBase, IQueryHandler, IRefresh
    {
        public override string Description { get { return "Show all tasks returned from a query in categorized lanes."; } }
        public string Name { get { return "Simple Task viewer (CMMI 6.0 Preview)";} }

        protected override void OnActivate()
        {
            base.OnActivate();
            DisplayName = Name;
            ItemHeight = 125;
            ItemWidth = 200;
            FontSize = 12;
        }

        private QueryDefinition _queryDefinition;
        public QueryDefinition QueryDefinition
        {
            get { return _queryDefinition; }
            set
            {
                _queryDefinition = value;
                NotifyOfPropertyChange(() => QueryDefinition);

                Refresh();
            }
        }

        private List<WorkItemNode> _workItemCollection;
        public List<WorkItemNode> WorkItemCollection
        {
            get { return _workItemCollection; }
            set
            {
                _workItemCollection = value;
                NotifyOfPropertyChange(() => WorkItemCollection);
                NotifyOfPropertyChange(() => ProposedTasks);
                NotifyOfPropertyChange(() => ActiveTasks);
                NotifyOfPropertyChange(() => ResolvedTasks);
                NotifyOfPropertyChange(() => ClosedTasks);
            }
        }

        public new void Refresh()
        {
            WorkItemCollection = QueryHelper.FetchItems(WorkItemStore, QueryDefinition);
            LastUpdated = DateTime.Now;
            base.Refresh();
        }

        public List<WorkItemNode> ProposedTasks
        {
            get { return WorkItemCollection == null ? null : FilterTasks("Proposed"); }
        }

        public List<WorkItemNode> ActiveTasks
        {
            get { return WorkItemCollection == null ? null : FilterTasks("Active"); }
        }
        
        public List<WorkItemNode> ResolvedTasks
        {
            get { return WorkItemCollection == null ? null : FilterTasks("Resolved"); }
        }
        
        public List<WorkItemNode> ClosedTasks
        {
            get { return WorkItemCollection == null ? null : FilterTasks("Closed"); }
        }

        private List<WorkItemNode> FilterTasks(string taskState)
        {
            return QueryHelper.FilterTasks(WorkItemCollection, "Task", taskState);
        }

        private DateTime _lastUpdated;
        public DateTime LastUpdated
        {
            get { return _lastUpdated; }
            set
            {
                _lastUpdated = value;
                NotifyOfPropertyChange(() =>LastUpdated);
            }
        }

        private WorkItemStore _workItemStore;
        public WorkItemStore WorkItemStore
        {
            get { return _workItemStore; }
            set 
            { 
                _workItemStore = value; 
                NotifyOfPropertyChange(() => WorkItemStore);
            }
        }

        private int _itemHeight;
        public int ItemHeight
        {
            get { return _itemHeight; }
            set
            {
                _itemHeight = value;
                NotifyOfPropertyChange(() => ItemHeight);
            }
        }

        private int _itemWidth;
        public int ItemWidth
        {
            get { return _itemWidth; }
            set
            {
                _itemWidth = value;
                NotifyOfPropertyChange(() => ItemWidth);
            }
        }

        private int _fontSize;
        public int FontSize
        {
            get { return _fontSize; }
            set
            {
                _fontSize = value;
                NotifyOfPropertyChange(() => FontSize);
            }
        }
    }
}
